package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.exception.MyException;
import com.itheima.common.result.JsonResult;
import com.itheima.common.utils.BaseContext;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengbinghua
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/addressBook")
@Api(tags = "地址管理")
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
@PostMapping
@ApiOperation(value = "添加地址")
    public JsonResult addAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return JsonResult.success("添加地址成功！");
    }
    @PutMapping
    @ApiOperation(value = "修改地址")
    public JsonResult updateAddressBook(@RequestBody AddressBook addressBook){
    addressBookService.updateById(addressBook);
    return JsonResult.success("修改地址成功！");
    }

    @DeleteMapping
    @ApiOperation("删除地址")
    public JsonResult delAddressBook(String ids){
    //分割字符串
        String[] split = ids.split(",");
        //批量删除
        addressBookService.removeBatchByIds(Arrays.asList(split));
        return JsonResult.success("删除成功！");
    }
    @ApiOperation(value = "获得地址列表")
    @GetMapping
    public JsonResult getAddressBookList(){
        List<AddressBook> list = addressBookService.list();
        return JsonResult.success(list);
    }
    @ApiOperation(value = "根据id获取地址")
    @GetMapping("{id}")
    public JsonResult getAddressBookById(@PathVariable String id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook==null){
            throw new MyException(404, "地址不存在！");
        }
        return JsonResult.success("查找成功！");
    }
    /***获取默认收货地址
     *
     * @return
     */
    @GetMapping("default")
    @ApiOperation(value = "获得默认收货地址")
    public JsonResult getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,"1");
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (null==addressBook){
            throw new MyException(404, "默认地址不存在！");
        }
        return JsonResult.success(addressBook);
    }
/**
 * 设置默认地址
 *
 * @return
 */
@GetMapping("default")
@ApiOperation("设置默认收货地址")
public JsonResult setDefaultAddressBook(@RequestBody AddressBook addressBook){
    LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
    wrapper.eq(AddressBook::getIsDefault,"0");
    //修改原来的默认地址
    addressBookService.update(wrapper);
    //设置默认地址
    addressBook.setIsDefault("1");
    addressBookService.updateById(addressBook);
    return JsonResult.success(addressBook);

}
}
